﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.Web.UserControls;
using BEGDC.CC.BL.CCFlow.CR;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.Common;
using BEGDC.Utility.Castle;
using BEGDC.CC.Web.Control;
using BEGDC.CC.BL.UserManagement;

namespace BEGDC.CC.Web.Common
{
    /// <summary>
    /// Summary description for AccompanyInput.
    /// </summary>
    public class CommonAssignmentForCV : System.Web.UI.Page
    {
        #region Variables
        protected System.Web.UI.WebControls.Button Btn_Submit;
        protected System.Web.UI.HtmlControls.HtmlTableRow TR_CATDirector;
        protected System.Web.UI.HtmlControls.HtmlTableRow TR_CATVP;
        protected System.Web.UI.HtmlControls.HtmlTableRow TR_PSO;
        protected System.Web.UI.HtmlControls.HtmlTableRow TR_BD;
        protected System.Web.UI.WebControls.DropDownList Dp_CATDirector;
        protected System.Web.UI.WebControls.DropDownList Dp_CATVP;
        protected System.Web.UI.WebControls.DropDownList Dp_PSOHead;
        protected System.Web.UI.WebControls.DropDownList Dp_BDHead;
        protected System.Web.UI.WebControls.DropDownList Dp_FNCController;
        protected System.Web.UI.HtmlControls.HtmlTableRow TR_FNC;
        protected System.Web.UI.HtmlControls.HtmlTableCell txtHint;
        protected string _Dept = "";
        protected string _Branch = "";
        protected string _Employee = "";
        #endregion

        private void Page_Load(object sender, System.EventArgs e)
        {
            BuildFormProperties();
            if (!this.IsPostBack)
            {
                Session["CATDirector"] = null;
                this.Btn_Submit.Attributes["onclick"] = "javascript:OnSubmit();";
                //his.Btn_Submit.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/bbw_submit.gif";

                InitForm();
            }
        }

        #region Form Properties
        public string Department
        {
            set
            {
                _Dept = value;
            }
            get
            {
                return _Dept;
            }
        }
        public string Branch
        {
            set
            {
                _Branch = value;
            }
            get
            {
                return _Branch;
            }
        }

        public string EmployeeID
        {
            set
            {
                _Employee = value;
            }
            get
            {
                return _Employee;
            }
        }
        #endregion

        private void BuildFormProperties()
        {
            //DEPARTMENT
            string DeptStr = Request.QueryString["Department"];
            if (DeptStr == null || DeptStr == String.Empty)
            {
                WebUtility.Go2ErrorPage(this.Page, "wrong Form Properties");
            }
            this.Branch = DeptStr.Substring(0, DeptStr.IndexOf("_"));
            this.Department = DeptStr.Substring(DeptStr.IndexOf("_") + 1);

            //Employee ID
            string EmployeeID = Request.QueryString["EmployeeID"];
            if (EmployeeID == null || EmployeeID == String.Empty)
            {
                EmployeeID = "";
            }
            this.EmployeeID = EmployeeID;

            if (this.EmployeeID != "")
            {
                IUser userService = AppContainer.Instance[ServiceConst.UserService] as IUser;
                Employee someone = userService.GetUserByID(EmployeeID);

                this.Department = someone.Department.DepartmentType.DicItemKey;
                this.Branch = someone.Department.Id.ToString();
            }
        }

        #region InitForm
        private void InitForm()
        {
            this.TR_CATDirector.Visible = true;
            if (this.Department == "CAT")
                WebUtility.BuildAssignmentDropDown(this.Dp_CATDirector, BEGDC.CC.BL.Workflow.UserRole.CATDirector, Branch);
            else if (this.Department == "Branch")
                WebUtility.BuildAssignmentDropDown(this.Dp_CATDirector, BEGDC.CC.BL.Workflow.UserRole.PSO, Branch);
            else
                WebUtility.BuildAssignmentDropDown(this.Dp_CATDirector, BEGDC.CC.BL.Workflow.UserRole.DeptDirector, Branch);

            if (this.Dp_CATDirector.Items.Count == 0)
            {
                string innerHtml = "&nbsp;&nbsp;There aren't any CODStaffs. If you want to assign the request,<br/> please contact role admin to add one.";
                innerHtml += " <br/> COD成员为空，如果您想分配该申请，请联系角色管理员.";
                this.txtHint.InnerHtml = innerHtml;

            }
        }
        #endregion

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Btn_Submit.Click += new EventHandler(Btn_Submit_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        #region Submit
        void Btn_Submit_Click(object sender, EventArgs e)
        {
            Session["CATDirector"] = this.Dp_CATDirector.SelectedValue.ToString();
            Response.Write("<script>window.returnValue='%reload%';window.close();</script>");
        }
        #endregion
    }
}
